import {createRouteMap} from './create-route-map';
import {creatRoute} from './history/base';

export default function createMatcher (routes) {
  let {pathMap} = createRouteMap (routes);
  console.log ('pathmap', pathMap);
  // 动态添加路由
  // 如果是用户使用 addRoutes(routes) 的方式添加，同样将参数 routes 添加进基本映射表中；
  function addRoutes (routes) {
    createRouteMap (routes, pathMap);
  }

  // 根据路径到路由映射表中找到对应的路由
  function match (path) {
    console.log ('matchpath---', path);
    let record = pathMap[path];
    if (record) {
      return creatRoute (record, {
        path,
      });
    } else {
      console.warn ('当前路由不存在');
    }
  }

  return {
    addRoutes,
    match,
  };
}
